from django.shortcuts import render

# Create your views here.
import json
from .serializers import *
from rest_framework.generics import ListAPIView,RetrieveAPIView
from rest_framework.views import APIView
from rest_framework.response import Response



class HostCityAPIView(ListAPIView):
    '''热门城市'''
    queryset = City.objects.filter(ishot=1)
    serializer_class = HostCityModelSerializer


class RecommendAPIView(ListAPIView):
    '''推荐职位'''
    queryset = Recruit.objects.filter(state=1)
    serializer_class = RecommendModelSerializer


class LatestAPIView(ListAPIView):
    '''最新职位'''
    queryset = Recruit.objects.filter().order_by('-id')
    serializer_class = RecommendModelSerializer


class EnterpriseAPIView(ListAPIView):
    '''热门企业'''
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseModelSerializer


class SearchAPIView(APIView):
    '''搜索职位'''
    def post(self,request):
        dict = json.loads(request.body.decode())
        name = dict.get('name')
        keyword = dict.get('keyword')

        if name and keyword:
            # data = Recruit.objects.all()
            data = Recruit.objects.all()

            serializers = RecommendModelSerializer(data,many=True)

            return Response(serializers.data)

        elif name:
            data = Recruit.objects.filter(city=name)

            serializers = RecommendModelSerializer(data,many=True)

            return Response(serializers.data)

        else:
            data = Recruit.objects.filter(jobname__contains=keyword)

            serializers = RecommendModelSerializer(data,many=True)

            return  Response(serializers.data)


class JobDetailsAPIView(RetrieveAPIView):
    '''职位详情'''
    queryset = Recruit.objects.all()
    serializer_class = RecommendModelSerializer


class EnterpriseDetailsAPIView(RetrieveAPIView):
    '''企业详情'''
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseModelSerializer1


class JobVisitAPIView(APIView):
    '''增加职位访问次数'''
    def put(self,request, pk):

        job = Recruit.objects.get(pk=pk)

        job.visits = job.visits + 1

        job.save()

        return Response({'message':'更新成功', 'success':True})


class EnterpriseVisitAPIView(APIView):
    '''增加企业访问次数'''

    def put(self, request, pk):
        enterprise = Enterprise.objects.get(pk=pk)

        enterprise.visits = enterprise.visits + 1

        enterprise.save()

        return Response({'message': '更新成功', 'success': True})


class CollectRecruitsAPIView(APIView):
    '''收藏职位'''
    def post(self,request, pk):
        recruits = Recruit.objects.get(pk=pk)
        user = User.objects.get(username=request.user.username)
        recruits.users.add(user)
        recruits.save()

        return Response({'message':'收藏成功', 'success':True})


class CancelCollectRecruitsAPIView(APIView):
    '''取消职位收藏'''
    def post(self,request, pk):
        recruits = Recruit.objects.get(pk=pk)
        user = User.objects.get(username=request.user.username)
        recruits.users.remove(user)
        recruits.save()

        return Response({'message':'取消收藏成功', 'success':True})


class CollectEnterpriseAPIView(APIView):
    '''收藏企业'''
    def post(self,request, pk):
        recruits = Enterprise.objects.get(pk=pk)
        user = User.objects.get(username=request.user.username)
        recruits.users.add(user)
        recruits.save()

        return Response({'message':'收藏成功', 'success':True})


class CancelCollectEnterpriseAPIView(APIView):
    '''取消收藏企业'''
    def post(self,request, pk):
        recruits = Enterprise.objects.get(pk=pk)
        user = User.objects.get(username=request.user.username)
        recruits.users.remove(user)
        recruits.save()

        return Response({'message':'收藏成功', 'success':True})